Parameter, Argument, Attribute 的区别

Parameter

对应中文即形参,临时变量,在函数定义中用于接受外部传入变量。

1
2
def foo(name, age): # name, age 即形参
pass

Argument

对应中文即实参,在调用函数时传入的值

1
foo('Tom', 11) # 'Tom', 11即实参

Atrribute

对应中文即属性,可以是一个类的变量(实例变量或者类变量)或者函数(成员函数)。 类的每一个实例都可以访问这些属性

1
2
3
4
5
6
7
8
9
10
11
12
13
class Color:
description = "This is a color" # class attributes

def __init__(self, color):
self.color = color

def do_something(self):
pass

c = Color('blue')
c.color # instance attribute
Color.description # class attribute
c.do_something() # instance attribute